<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <head th:include="commom::title('高青民康养护院')"></head>
</head>

<body>
<div class="layuimini-container">
    <div class="layuimini-main">

        <fieldset class="table-search-fieldset">
            <legend>搜索信息</legend>
            <div style="margin: 10px 10px 10px 10px">
                <div class="layui-form-item">
                    <!-- 页面顶部搜索条件 -->
                    <div class="layui-inline layui-form-pane">
                        <label class="layui-form-label">管理员姓名</label>
                        <div class="layui-input-inline">
                            <input type="text" id="adminName" name="adminName" value="" autocomplete="off"
                                   class="layui-input">
                        </div>
                    </div>
                    <div class="layui-inline">
                        <button type="submit" id="search" class="layui-btn layui-btn-primary" lay-submit
                                lay-filter="data-search-btn">
                            <i class="layui-icon"></i> 搜 索
                        </button>
                    </div>
                </div>
            </div>
        </fieldset>

        <!-- 头 工 具 栏 -->
        <script type="text/html" id="toolbarDemo" th:if="${session.admin} != null">
            <div class="layui-btn-container">
                <button class="layui-btn layui-btn-normal layui-btn-sm data-add-btn" lay-event="add"> 添加</button>
                <button class="layui-btn layui-btn-sm layui-btn-danger data-delete-btn" id="delete" lay-event="delete">
                    删除
                </button>
            </div>
        </script>

        <!-- 表 格 信 息 -->
        <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>

        <!-- 行 工 具 栏 -->
        <script type="text/html" id="currentTableBar">
            <a th:if="${session.admin} != null" class="layui-btn layui-btn-normal layui-btn-xs data-count-edit"
               lay-event="edit">编辑</a>
            <a th:if="${session.admin} != null" class="layui-btn layui-btn-xs layui-btn-danger data-count-delete"
               lay-event="delete">删除</a>
            <a th:if="${session.admin} == null"
               class="layui-btn layui-btn-xs layui-btn-danger data-count-delete">无权限</a>
        </script>
    </div>
</div>
<script th:src="@{/static/lib/layui-v2.5.5/layui.js}" charset="utf-8"></script>
<script>
    layui.use(['form', 'table', 'layer', 'laypage'], function () {
        const $ = layui.jquery,
            form = layui.form,
            layer = layui.layer,
            table = layui.table;

        table.render({
            elem: '#currentTableId',
            id: 'testReload',
            url: '/account/adminList',
            limits: [5, 10, 15, 20],
            limit: 10,
            page: true,
            toolbar: '#toolbarDemo',
            defaultToolbar: ['filter', 'exports', 'print', {
                title: '提示',
                layEvent: 'LAYTABLE_TIPS',
                icon: 'layui-icon-tips'
            }],
            // 页面上要展示的列
            cols: [
                [
                    {type: "checkbox", field: 'center'},
                    {field: 'loginId', width: 120, title: '登录账号', sort: true, align: 'center'},
                    {field: 'loginName', width: 120, title: '名称', sort: true, align: 'center'},
                    {field: 'telPhone', width: 120, title: '电话', sort: true, align: 'center'},
                    {field: 'sex', width: 80, title: '性别', sort: true, align: 'center'},
                    {field: 'courtyard', width: 120, title: '院区', sort: true, align: 'center'},
                    {
                        // 修改展示时间【2023-05-11T20:25:23.000+00:00修改为2023-05-11 20:25:23】
                        field: 'createTime', width: 180, title: '创建时间', sort: true, align: 'center',
                        templet: function (d) {
                            var originalTime = d.createTime; // 获取原始时间数据
                            var dateObj = new Date(originalTime); // 将时间字符串转换为日期对象

                            // 使用日期对象的方法获取各个部分的值
                            var year = dateObj.getFullYear();
                            var month = ("0" + (dateObj.getMonth() + 1)).slice(-2);
                            var day = ("0" + dateObj.getDate()).slice(-2);
                            var hours = ("0" + dateObj.getHours()).slice(-2);
                            var minutes = ("0" + dateObj.getMinutes()).slice(-2);
                            var seconds = ("0" + dateObj.getSeconds()).slice(-2);

                            // 返回拼接好的新的时间格式
                            return year + "-" + month + "-" + day + " " + hours + ":" + minutes + ":" + seconds;
                        }
                    },
                    {
                        field: 'updateTime', width: 180, title: '更新时间', sort: true, align: 'center',
                        templet: function (d) {
                            var originalTime = d.createTime; // 获取原始时间数据
                            var dateObj = new Date(originalTime); // 将时间字符串转换为日期对象

                            // 使用日期对象的方法获取各个部分的值
                            var year = dateObj.getFullYear();
                            var month = ("0" + (dateObj.getMonth() + 1)).slice(-2);
                            var day = ("0" + dateObj.getDate()).slice(-2);
                            var hours = ("0" + dateObj.getHours()).slice(-2);
                            var minutes = ("0" + dateObj.getMinutes()).slice(-2);
                            var seconds = ("0" + dateObj.getSeconds()).slice(-2);

                            // 返回拼接好的新的时间格式
                            return year + "-" + month + "-" + day + " " + hours + ":" + minutes + ":" + seconds;
                        }
                    },
                    {fixed: 'right', width: 200, align: 'center', title: '操作', toolbar: '#currentTableBar'}
                ]
            ]
        });

        // 顶部搜索
        $('#search').on('click', function () {
            // 搜索条件
            var loginName = $('#loginName').val();
            table.reload('testReload', {
                method: 'post'
                , where: {
                    'loginName': loginName
                }
                , page: {
                    curr: 1
                }
            });
        });

        /** 监听头工具栏 */
        table.on('toolbar(currentTableFilter)', function (obj) {
            const data = obj.data //获得当前行数据
                , layEvent = obj.event; //获得 lay-event 对应的值
            if (obj.event === 'add') {  // 监听添加操作
                const index = layer.open({
                    title: '添加管理员',
                    type: 2,
                    shade: 0.2,
                    maxmin: true,
                    shadeClose: true,
                    area: ['600px', '320px'],
                    content: '/view/addAdmin'
                });
            } else if (obj.event === 'delete') {
                var checkStatus = table.checkStatus('testReload');
                var selectCount = checkStatus.data.length;
                console.log(checkStatus);
                console.log(selectCount);
                if (selectCount == 0) {
                    layer.msg('批量删除至少选中一项数据', function () {
                    });
                    return false;
                }
                layer.confirm('真的要删除选中的项吗？', function (index) {
                    layer.close(index);
                    // 加载层
                    index = layer.load(1, {shade: [0.1, '#fff']});
                    var listStr = "";
                    for (var i = 0; i < selectCount; i++) {
                        listStr = listStr + "," + checkStatus.data[i].adminId;
                    }
                    $.ajax({
                        type: 'post',
                        data: {"listStr": listStr},
                        url: '/account/batchDelAdmin',
                        success: function (data) {
                            layer.close(index);
                            layer.msg(data.msg);
                            table.reload('testReload');
                        }
                    });
                })
            }
        });


        table.on('tool(currentTableFilter)', function (obj) {
            var data = obj.data;
            if (obj.event === 'edit') {
                var index = layer.open({
                    title: '编辑用户',
                    type: 2,
                    shade: 0.2,
                    maxmin: true,
                    shadeClose: true,
                    area: ['600px', '320px'],
                    content: '/account/modifyAdmin?adminId=' + data.adminId,
                });
                return false;
            } else if (obj.event === 'delete') {
                layer.confirm('真的删除行么', function (index) {
                    del(data.adminId, obj, index);
                });
            }
        });

        function del(adminId, obj, index) {
            $.ajax({
                url: '/account/delAdmin?adminId=' + adminId,
                dataType: 'json',
                type: 'post',
                success: function (data) {
                    obj.del(); //删除对应行（tr）的DOM结构
                    layer.msg(data.msg, {icon: 1, time: 1000});
                    layer.close(layui.index);
                    table.reload('testReload');
                }
            })
        }
    });
</script>

</body>
</html>
